<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentRate.class.php, $Author:  $ */



/** Component for rating images
 *
 * @version $Id: PhpFox_ComponentRate.class.php 416 2008-03-28 07:20:51Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentRate extends BaseComponent
{
    function process()
    {
        $oGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oGalleryItem = $this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecuritySevice PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = $this->_oModule->getTemplate();
        $oReq = App::getRequest();
        App::loadClass('Filter');
        $aOut = array();

        $oSecurityService->checkAccess('rate');

        $iRateId = $oReq->get('rateid');

        $iPage = $oReq->getInt('page', 1);

        $oSecurityService->checkPerm('gallery.rate', array('full', 'read_only'));

        $sUser = $oSecurityService->getCurrentUserLogin();

        if ($iRating = $oReq->get('rating'))
        {
        	App::membersOnly();

        	$iId = $oReq->get('id');

            $oSecurityService->checkPerm('gallery.rate', array('full'));
            $oRatingItem = $this->_oModule->getItem('Rating');
            /* @var $oRatingItem PhpFox_Mod_Gallery_ItemRating */

            $oRatingItem->aData = array
            (
            	'main'   => $iId,
            	'user'   => $sUser,
            	'rating' => $oReq->getInt('rating'),
            	'time'   => time(),
            );
            $oRatingItem->insert();
            $oGalleryService->updateRating($iId);
        }

        $aCategories = array();
        $aCategories[''] = App::format('option.select');
        foreach (App::getOptions('image_cat') as $sCategoryId => $sCategoryText)
        {
            $aCategories[$sCategoryId] = $sCategoryText;
        }
        $aCond = array
        (
        	'category' => array
        	(
        		'value'  => '=',
        		'select' => $aCategories,
        	),
        );
        $oFilter = &new Filter('rate', $aCond);
        $oTpl->assign(array(
        	'aFilter'     => $oFilter->aHtml,
        	'sFilterName' => 'rate',
        ));
        $aOut['filter'] = $oTpl->fetch('GroupFilter.html');
        $aCond = array();
        if ( $iRateId )
        {
        	$aCond[] = 'g.id = "'. $this->oDatabase->escape($iRateId) .'"';
        }
        $aCond[] = 'g.allow = ""';
        $aCond[] = 'g.who_view = ""';
        foreach ($oFilter->aConditions as $sCond)
        {
        	$sCond = preg_replace('#{\#(.*)}#i', ' g.\\1', $sCond);
            $aCond[] = $sCond;
        }

		$sConditions = "";
		if ( isset($aCond) && count($aCond) > 0 )
		{
        	$sConditions = " AND " . implode(" AND ", $aCond);
        }

        $aItem = $this->oDatabase->getRow('SELECT g.id, g.view, g.time, g.text, g.rating, g.comment, u.user FROM
        '. App::getT('main') .' AS g
        LEFT JOIN '. App::getT('user') .' AS u ON(g.user_id = u.id)
        WHERE 1 '. $sConditions .'
        ORDER BY RAND() LIMIT 1');

        if ( !count($aItem) )
        {
            $oTpl->assign('bNoPics', 1);
            $aOut['main'] = $oTpl->fetch('Rate.html');
            return $aOut;
        }
        $bRated = 0;

        if ($oGalleryService->isPictureRated($aItem['id']))
        {
            $bRated = 1;
        }

        if (!$sUser)
        {
            $bRated = 0;
        }

		$sActualImage = App::getParam('gallery.pic.url') . $aItem['id'] . '_view.jpg';
		if (!file_exists(App::getParam('gallery.pic.path') . $aItem['id'] . '_view.jpg'))
		{
			$sActualImage = App::getParam('gallery.pic.url') . $aItem['id'] . '.jpg';
		}        
        
        $oTpl->assign(array(
        	'sActualImage' => $sActualImage,
        	'sUImageUrl' => App::getParam('user.pic.url'),
        	'sGImageUrl' => App::getParam('gallery.pic.url'),
        	'bRated'     => $bRated,
            'iNextPage'  => $iPage + 1,
        	'aItem'      => $aItem,
        ));
        $aOut['main'] = $oTpl->fetch('Rate.html');
        return $aOut;
    }
}
?>